package com.iweb.web.servlet;

import com.iweb.commons.utils.BeanUtil;
import com.iweb.commons.utils.ModelAndView;
import com.iweb.commons.utils.UrlUtil;
import com.iweb.pojo.Emp;
import com.iweb.web.controller.EmpController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: jack
 * @date: 2022-08-04 0004
 * @ApiNOTE:
 */
@WebServlet(urlPatterns = "/emp/*", loadOnStartup = 1)
public class EmpServlet extends HttpServlet {

  @Override
  protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String action = UrlUtil.action(req, "/emp/");

    if ("login".equals(action)) {
      login(req, resp);
    }

  }

  private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    /*
    account: admin
    password: admin
     */
    String account = req.getParameter("account");
    String password = req.getParameter("password");
    Emp emp = new Emp();
    emp.setAccount(account);
    emp.setPassword(password);

    EmpController empController = BeanUtil.getBean(getServletContext(), "emp");
    ModelAndView mv = empController.login(emp, req.getSession());
    mv.render(req, resp, mv);

  }
}
